const app = getApp()
Page({
    data: {
            shops:{},
            prudects:[],
            page:1,
            totalPage: ''
    },
    onLoad(query) {
        let shops = JSON.parse(query.param)
        console.log("商家信息", query.param)
        shops.image = JSON.parse(shops.image)

        this.setData({
            shops: shops
        })
        this.getShop()
        app._init_(this)
    },
    // 跳转
    navigateTo(e){
        app.navigateTo(e)
    },

    getShop(){
        //   /product/app/page
        let params = {
                page:this.data.page,
                sid:this.data.shops.id,
            };
        app.product.shopsPage(params)
        .then(json=>{
            console.log(json)
            this.dataPromise(json.data.list)
            .then(res => {
                console.log(res)
                let list = [...this.data.prudects, ...res]
                this.setData({ prudects: list, totalPage: json.data.totalPage })
            })
        })
    },
    // 转价格
    dataPromise(list) {
        return new Promise((relose, reject) => {
            list.forEach((item) => {
                item.category = JSON.parse(item.category)
                item.colors = JSON.parse(item.colors)
                item.match = JSON.parse(item.match)
                item.params = JSON.parse(item.params)
                item.slideshows = JSON.parse(item.slideshows)
                item.img = item.slideshows[0]
                item.title = item.tit
                this.getminPrice(item.params)
                    .then(res => {
                        console.log("==============================", res)
                        item.price = res
                    })
            })
            relose(list)
        })
    },

    getminPrice(params) {
        return new Promise((resole, reject) => {
            let prices = []
            for (let i = 0; i < params.length; i++) {
                for (let t = 0; t < params[i].term_list.length; t++) {
                    for (let s = 0; s < params[i].term_list[t].specifications.length; s++) {
                        let price = (params[i].term_list[t].specifications[s].money / params[i].term_list[t].value / 100).toFixed(2)
                        prices.push(price)
                    }
                }
            }
            let minprice = Math.min.apply(null, prices);
            resole(minprice)
        })
    },

   //   下拉加载
    onReachBottom() {
        console.log("11111111111")
        let that = this
        // 判断是否还有数据需要加载
        if (that.data.page < that.data.totalPage) {
            that.setData({ show: true });
            const newPage = that.data.page + 1;
            that.setData({ page: newPage });
            that.getShop();
        } else {
            that.setData({ show: false });
        }
    },
});
